HOW TO MAKE A PLAYER SHOOT BY CHRONIC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Contents
~~~~~~~~

1. Introduction
2. Single Direction Shooting
3. Using a variable for multi-directional shooting
4. Creating a pause between shots
5. Destroying the bullet if it goes out of view

-----

INTRODUCTION
~~~~~~~~~~~~

This question gets asked an awful lot on the forum, so here is a tutorial for you people that still don't understand it or arn't getting any help.

A projectile/bullet is nothing more that another object created at a certain place programmed to move in a certain direction and destroys the other object it collides with.

If your bullet only need to travel in a one direction all the time like in a space invader style game, then life is easy for you, but for more complex shooting then things get a little harder.

The direction of anything is similar to a compass, use this for reference when choosing your bullets direction.

           90
           -Y
            |
      180 --+-- 360/0
       -X   |   +X
           270
           +Y

-----

SINGLE DIRECTION SHOOTING
~~~~~~~~~~~~~~~~~~~~~~~~~

Using the direction compass at the start of this tutorial choose which direction you want your bullet to travel in.

In the CREATE section of your bullet place a 'Set direction and speed of motion' icon or more commonly known as 'the blue arrow icon' (i wonder why?) and then enter the relevent direction number in the 'direction' box and then the speed you want the bullet to move in the 'speed' box, don't make this RELATIVE because the direction won't be what you set it as.

Now in the player object put a 'Create an instance of an object' (the lightbulb) icon in your 'fire' key and set it to create your bullet at X = 0, Y = 0 this will create the bullet at the top left corner of the room unless you tick the RELATIVE box, so go ahead and tick it.

Now by testing your game and firing you'll see that the bullet is created from the top left corner of the player's sprite.

To fix this you need to mess around with the X and Y in the create instance icon.

Finding out the height and width of your player sprite will make it eayser for you to create the bullet in the correct place.

For example:

If your player is always facing/shooting up and you dimensions of your player sprite width is 20 and the bullet sprite width is 6, then by halfing the width of the player sprite to get the center, in this case 10, then half the width of the bullet, 3, then minus them 10 - 3 = 7, then change X value of the creation icon to 7 will make the bullet fire centered... but the bullet will overlap still on the Y axis, to fix that just find the height of the bullet sprite and then set the Y axis to minus the bullet sprite height, which would give you...

      X = 7    Y = -6

The bullet should now be created correctly without overlapping.

-----

USING A VARIABLE FOR MULTI-DIRECTIONAL SHOOTING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A variable can also be used set the direction of the bullet, in this section we will use the variable IMAGE_SINGLE to set the bullets direction.

IMAGE_SINGLE is a command used to set a sprite to use a certain frame index in the sprite.

Create a sprite with the player facing in all 4 directions (up, down, left and right)

NOTE: If you want your player animated then 4 different sprites will need to be created instead of 1 sprite with the 4 directions, also this technique won't work using more than one sprite.


In frame 1 of your sprite make the player face up and rotate clockwise.

In the directions keys for you player add a X=2 icon and put 'image_single' for the variable, the value will be different depending on the key...
 
      Key   | Value
      -------------
      UP    |  0
      RIGHT |  1
      DOWN  |  2
      LEFT  |  3

Like before add the 'Create instance' (lightbulb) icon to your 'fire' key

Now in the CREATE section of the bullet this script needs to be placed, copy everything between the *'s

*********
{
 if (player.image_single = 0)
 {
  x = player.x+player.sprite_width/2-[HBW];
  y = player.y-[BH];
  direction = 90;
 }
 if (player.image_single = 1)
 {
  x = player.x+player.sprite_width;
  y = player.y+player.sprite_height/2-[HBH];
  direction = 0;
 }
 if (player.image_single = 2)
 {
  x = player.x+player.sprite_width/2-[HBW];
  y = y+player.sprite_height;
  direction = 270;
 }
 if (player.image_single = 3)
 {
  x = player.x-[BW];
  y = player.y+player.sprite_height/2-[HBH];
  direction = 180;
 }
 speed = 2
}
*********

You'll need to make some changes where the [ ]'s are, use the key below.

[BW]  = Bullets Width
[BH]  = Bullets Height
[HBW] = Half Bullets Width
[HBH] = Half Bullets Height


When you add your values DON'T use the [ ]'s just put the number.

Now let me explain that script a little... 


 if (player.image_single = 0)
 {
  x = player.x+player.sprite_width/2-[HBW];
  y = player.y-[BH];
  direction = 90;
 }


Look at the first line of the script, this is the part that checks if something is TRUE, which for us its checking if the players image_single is equal to 0, if it does equal 0, then this means its TRUE and would then execute the script within the { }'s after it, else it would skip that section of code and check the next part of it.

Now lets pretend its true and checking the next part of the script within the { }'s.

  x = player.x+player.sprite_width/2-[HBW];
  y = player.y-[BH];
  direction = 90;

The first line of the code changes the X axis of the object the code is running from, its not a very complex equation but lets take a closer look...

NOTE: i've replaced the [HBW] with the value of half the bullets width

player.x+player.sprite_width/2-3;

 "player.x"                 - Returns the value of the X axis for the player object
 "player.sprite_width/2-3"  - Returns the width of the sprite for the player object and then divides it by 2, then minus 3

Ok thats the first line out the way with... lets look at the next equation...

NOTE: i've replaced the [BH] with the value of bullets height

  y = player.y-6;

 "player.y-6"               - Returns the valus of the Y axis for the player object then minus 6.

The reason we've used variables like player.x is because we cannot use a fixed number because the X and Y of the player is changing as the player moves around the room/level. If we did use fixed numbers like below...

  x = 100+12-3;
  y = 100-6;

this would make the bullets X and Y axis (because thats the object the script is running from) always be 'X = 103 and Y = 94' (the value of the equations)

The remaining parts of the script are sligtly different but still use the same 'rules', the last line of the script "speed = 2" sets the speed that the bullet moves at, increasing this number will increase the speed.

-----

CREATING A PAUSE BETWEEN SHOTS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Both of the examples in this tutorial will cause a 'rapid fire' effect, This is fixed by using ALARMS and changing the 'fire' key to this script

*********
{
 if (canshoot = 1)
 {
  instance_create(x, y, bullet);
  alarm[0] = room_speed/2;
  canshoot = 0;
 }
}
*********

This script checks if canshoot = 1 and if its TRUE creates a bullet at the players current X,Y axis, sets ALARM 0 to half the rooms speed, then changes the canshoot variable to 0, which would mean the next time you press fire won't create the bullet because the check returns FALSE because canshoot no longer equals 1.

So thats where ALARM 0 comes in... put a X=2 icon in ALARM 0 with the following settings...

Variable  = canshoot
new value = 1

Instead of putting a number to set ALARM 0 in the 'fire' key, we use 'room_speed/2', this will create a bullet every half of a second because the value of room_speed is always equal to 1 real time second no matter what the value, using this, the only thing the room speed value will effect is the distance between each bullet.

Finaly you'll need to give canshoot a value to start off with or you'll get an error, so in the CREATE section of the player add another X=2 with the same settings as above.

-----

DESTROYING THE BULLET IF IT GOES OUT OF VIEW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only thing that remains to be done is to destroy the bullet if it goes out of the view. This is done very simply by putting a 'Destroy the instance' (trash can) icon in the OUTSIDE section of the bullet

-----

The examples covered in this tutorial are just some of the many ways to be able to make a player shoot, because everyone codes things differently, and also are making different games, its better if you think ahead of what you want to see in your game, for example, if you're gonna use different types of ammo/bullet then the way you make your player shoot will be different.





-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

This tutorial was created by Chronic of http://csos.topcities.com any suggestions or questions regarding this tutorial please email me at bluntman_667@btinternet.com with the tutorials title as the subject.

If you would like to submit any tutorials to me, then use the same email with the subject 'Tutorial Submition' or something to that effect.

This Tutorial may be distributed freely without charge in its original form, please give credit where credit is due.



